package com.axzhengxin.platform.business.service.impl;

import com.axzhengxin.platform.business.domain.BizApplyInfo;
import com.axzhengxin.platform.business.domain.BizApproveDetail;
import com.axzhengxin.platform.business.domain.BizApproveInfo;
import com.axzhengxin.platform.business.mapper.BizApplyInfoMapper;
import com.axzhengxin.platform.business.mapper.BizApproveDetailMapper;
import com.axzhengxin.platform.business.mapper.BizApproveInfoMapper;
import com.axzhengxin.platform.business.service.IBizApproveInfoService;
import com.axzhengxin.platform.common.util.IdUtils;
import com.axzhengxin.platform.homePage.domain.StatisticData;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author ahzj0005
 * @Date 2021/2/23-17:12
 **/
@Slf4j
@Service
public class BizApproveInfoServiceImpl implements IBizApproveInfoService {
    @Autowired
    private BizApproveInfoMapper bizApproveInfoMapper;
    @Autowired
    private BizApproveDetailMapper bizApproveDetailMapper;
    @Autowired
    private IdUtils idUtils;

    @Autowired
    private BizApplyInfoMapper bizApplyInfoMapper;
    @Override
    public BizApproveInfo geBizApproveInfo(String approveId) {
        return bizApproveInfoMapper.selectBizApproveInfoByApproveId(approveId);
    }

    @Override
    public AjaxResult insertApproveDetail(BizApproveDetail bizApproveDetail) {

        if (StringUtils.isBlank(bizApproveDetail.getApproveDetailId())){
            bizApproveDetail.setApproveDetailId(UUID.randomUUID().toString());
        }
        String approveId = bizApproveDetail.getApproveId();
        BigDecimal approveDetailApproveAmount = bizApproveDetail.getApproveAmount();
        BizApproveInfo bizApproveInfo = bizApproveInfoMapper.selectBizApproveInfoByApproveId(approveId);
        BigDecimal approveAmount = bizApproveInfo.getApproveAmount();
        BizApplyInfo bizApplyInfo = bizApplyInfoMapper.selectBizApplyInfoById(bizApproveInfo.getApplyId());
        BigDecimal financeAmount = bizApplyInfo.getFinanceAmount();
        if (approveAmount.add(approveDetailApproveAmount).compareTo(financeAmount)>0){
            return AjaxResult.error("授信金额设置过大！当前可授信余额为"+financeAmount.subtract(approveAmount).setScale(2, RoundingMode.HALF_UP)+"万元");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("approveId",approveId);
        map.put("amount",approveDetailApproveAmount);

        bizApproveInfoMapper.updateAmountByApproveId(map);
       int maxSort= bizApproveDetailMapper.selectMaxSortByApproveId(approveId);
       if (maxSort==0){
           maxSort=1;
       }
       bizApproveDetail.setDetailSort(maxSort);
        return AjaxResult.success(bizApproveDetailMapper.insertBizApproveDetail(bizApproveDetail));
    }

    @Override
    public int deleteBizApproveInfoById(String approveDetailId) {
        return bizApproveDetailMapper.deleteBizApproveDetailById(approveDetailId);
    }

    @Override
    public int insertApproveInfo(BizApproveInfo bizApproveInfo) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        String applyId = bizApproveInfo.getApplyId();
        BizApplyInfo bizApplyInfo = bizApplyInfoMapper.selectBizApplyInfoById(applyId);
        /*bizApproveInfo.setApproveId(UUID.randomUUID().toString());*/
        String snakeId = idUtils.getSnakeId();
        bizApproveInfo.setApproveId(snakeId);
        bizApproveInfo.setApplyId(bizApplyInfo.getReqId());
        bizApproveInfo.setCustomerId(bizApplyInfo.getCustomerId());
        bizApproveInfo.setCustomerName(bizApplyInfo.getCustomerName());
        bizApproveInfo.setCreateBy(userId.toString());
        bizApproveInfo.setCreateTime(new Date());
        int i = bizApproveInfoMapper.insertBizApproveInfo(bizApproveInfo);
        return i;
    }
}
